/*
 * Copyright 2006-2008 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.martinimix.context.event.syndication;

import net.martinimix.domain.syndication.SyndicationFeed;
import net.martinimix.service.syndication.feedburner.FeedBurnerService;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;

/**
 * Provides an syndication feed published event listener to notify FeeedBurner
 * of feed publishing events.
 * 
 * <p>
 * This listener requires that the following parameters be set:
 * </p>
 * 
 * <table width="85%" cellpadding="2" border="1">
 *   <tr>
 *     <td><b>username</b></td>
 *     <td>your organization's FeedBurner username</td>
 *   </tr>
 *   <tr>
 *     <td><b>password</b></td>
 *     <td>your organization's FeedBurner password</td>
 *   </tr>
 * </table>
 *  
 * @author Scott Rossillo
 *
 */
public class FeedBurnerSyndicationEventListener implements ApplicationListener, InitializingBean {
	
	private String username;
	private String password;
	private FeedBurnerService feedBurner;
	
	/**
	 * Creates a new FeedBurner syndication event listener.
	 */
	public FeedBurnerSyndicationEventListener() { }

	/**
	 * Creates a FeedBurner service object after username and password
	 * properties have been set.
	 * 
	 * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
	 */
	public void afterPropertiesSet() throws Exception {
		
		if(username == null) {
			throw new IllegalArgumentException("[username] must be set!");
		}
		
		if(password == null) {
			throw new IllegalArgumentException("[password] must be set!");
		}
		
		feedBurner = new FeedBurnerService();
		feedBurner.setUsername(username);
		feedBurner.setPassword(password);
	}

	/**
	 * Handles the given application event by calling {@link #onPublish(SyndicationFeedPublishedEvent)} if
	 * the given event is an instance of <code>SyndicationFeedPublishedEvent</code>.
	 * 
	 * @see org.springframework.context.ApplicationListener#onApplicationEvent(org.springframework.context.ApplicationEvent)
	 */
	public final void onApplicationEvent(ApplicationEvent event) {
		
		if(event instanceof SyndicationFeedPublishedEvent) {
			this.onPublish( (SyndicationFeedPublishedEvent) event ); 
		}
	}
	
	/**
	 * Updates FeedBurner's information about the syndication feed
	 * triggering the given event.
	 * 
	 * @param event the <code>SyndicationFeedPublishedEvent</code>
	 * containing information about the published <code>SyndicationFeed</code> 
	 */
	protected void onPublish(SyndicationFeedPublishedEvent event) {
		
		SyndicationFeed feed = event.getFeed();
		
		if(feedBurner.exists(feed)) {
			feedBurner.resync(feed);
		} else {
			feedBurner.add(feed, event.getUrl());
		}
	}

	/**
	 * Sets the FeedBurner user for this service.
	 * 
	 * @param username the FeedBurner user for this service
	 */
	public void setUsername(String username) {
		this.username = username;
	}

	/**
	 * Sets the FeedBurner account password for this service.
	 * 
	 * @param password the FeedBurner account password for this service
	 */
	public void setPassword(String password) {
		this.password = password;
	}
}
